import ResMgr from "../common/ResMgr";
import TickerMgr from "../ticker/TickerMgr";


export default class SimpleTip extends cc.Node {
    private static all: Array<SimpleTip> = [];
    private static clean: Array<SimpleTip> = [];
    private _label: cc.Label;
    private _bg: cc.Sprite;
    private _hei: number = 40;
    private _vy: number = 200;
    constructor() {
        super("SimpleTip");
        this.group = "UILayer";
        var node: cc.Node = new cc.Node;
        this.addChild(node);
        this._bg = node.addComponent(cc.Sprite);
        ResMgr.loadSpriteFrame("ui/MainGame/MainGameNew", "gq_tishi_dk", this._bg);
        node.width = 720;
        node.height = this._hei;
        node.y = 5;
        node.group = "UILayer";
        var node: cc.Node = new cc.Node;
        this.addChild(node);
        this._label = node.addComponent(cc.Label);
        this._label.fontSize = 30;
        this._label.node.y = -5
        this._label.node.color = cc.color(255,255,255,255);
        const outline = node.addComponent(cc.LabelOutline);
        outline.color = cc.color(0, 0, 0, 255);
        outline.width = 2;
    }

    public static create(content: string, time: number): SimpleTip {
        var instance: SimpleTip = null;
        if (!instance) {
            instance = new SimpleTip;
        }
        instance.init(content, time);
        return instance;
    }

    public init(content: string, time: number) {
        this._label.string = content;
        //距离上一次点击时间如果超过一定时间整体坐标Y向上
        if (time > 800) {
            for (let i = 0; i < SimpleTip.all.length; i++) {
                SimpleTip.all[i].y = SimpleTip.all.length > 4 ? SimpleTip.all[i].y + 200 : 20 + ((SimpleTip.all.length - i - 1) * 40);
                if (SimpleTip.all[i].y > 20 + ((SimpleTip.all.length - i - 1) * 40)) SimpleTip.all[i].y = 20 + ((SimpleTip.all.length - i - 1) * 40);
            }
        }
        for (var i: number = 0; i < SimpleTip.all.length; i++) {
            // SimpleTip.all[i].stopAllActions();
            SimpleTip.all[i].setScale(1);
            if (i > 0) {
                SimpleTip.all[i].y = SimpleTip.all[i - 1].y - 45;
            }
            cc.tween(SimpleTip.all[i]).by(.2, { y: 35 }, { easing: 'sineOut' }).start();
        }
        if (!this) return;
        SimpleTip.all.push(this);
        this.setScale(.1, .1);
        this.opacity = 255;
        if (SimpleTip.all.length >= 2) this.y = SimpleTip.all[SimpleTip.all.length - 2].y - 10;
        if (SimpleTip.all.length == 1) this.y = 0;
        cc.tween(this).to(.1, { scale: 1.1 }).to(.1, { scale: 1 }).start();
        this.runAction(cc.sequence(cc.delayTime(2), cc.callFunc(this.alpha.bind(this))));
    }

    public alpha() {
        if (!this) return;
        cc.tween(this).to(.6, { opacity: 0 }).call(() => {
            SimpleTip.all.splice(SimpleTip.all.indexOf(this), 1);
            this.recovery();
        }).start();
    }

    public recovery() {
        this.parent.removeChild(this);
    }
}